package com.um.jdy.app.charge.scheduled;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.um.jdy.app.charge.manager.OrderManager;
import com.um.jdy.common.charge.entity.po.Order;
import com.um.jdy.common.charge.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 分账
 * @author renjunwei
 * @since 2023/5/18
 * 版权所有 广州优匠科技有限公司
 */
@Slf4j
@Component
@Profile({"!dev"})
public class ProfitShargingTask {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderManager orderManager;

    /**
     * 查询处理中分账订单，每3分钟执行一次（首次为程序启动后60秒）
     */
    @Async
    @Scheduled(fixedRate = 180000, initialDelay = 60000)
    public void autoQuery() {
        log.info("执行查询处理中分账订单");
        List<Order> orderList = orderService.getListBySqlName("get_processing_profit_sharing_order_list", new Object[]{});
        for (Order order : orderList) {
            String orderNo = order.getOrderNo();
            try {
                orderManager.queryWxProfitSharingResult(order);
            } catch (Exception ex) {
                log.error(StrUtil.format("查询处理中分账订单{}出错", orderNo), ex);
            }
        }
        log.info("处理完成{}个订单", orderList.size());
    }
}
